//
//  XXSPMainViewController.m
//  ShangChengMoBan
//
//  Created by 张铁军 on 16/8/27.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "XXSPMainViewController.h"
#import "XXSPMainTableCell.h"
#import "XXSPInfoViewController.h"
#import "XXSPFenLeiTableView.h"
#import "XXSC_FirstModel.h"
#import "XXSPshopViewController.h"
@interface XXSPMainViewController ()<UITableViewDelegate,UITableViewDataSource,fenLeiTableDelegate>
{
    RWScrollView *_myTbaleView;
    XXSPFenLeiTableView *_fenLeiView;
    int _page;
    NSMutableArray *_shuJuArr;
    NSDictionary *_dic;
    UIView * _view;
}
@end

@implementation XXSPMainViewController
- (IBAction)allShop:(id)sender {
    _dic = nil;
    [_myTbaleView headerBeginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    _dic = nil;
    // Do any additional setup after loading the view.
    [self drawNavWithTitle:@"线下商铺" WithColor:[UIColor whiteColor]];
    [self drawBackButton];
    [self drawheaderOtherView];
    [self drawView];
   [self requestData:1];
    [TCUtils showLoadingWithView:self.view];
}
-(void)headRefresh{
    _page = 1;
    [self requestData:1];
}
-(void)footerRefresh{
    _page ++;
    [self requestData:0];
}
-(void)requestData:(int)first{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"10" forKey:@"pageSize"];
    [dic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"pageNum"];
    if (_dic!=nil) {
         [dic setObject:_dic[@"classifyId"] forKey:@"classifyId"];
    }
    [NetWorking NEWjieKouQingQiu:XXSC_getAlloffLinMall ANDIP:IP And:dic anBlock:^(NSDictionary *dic, NSError *error) {
        [TCUtils hiddonLoadingWithView:self.view];
        [_myTbaleView headerEndRefreshing];
        [_myTbaleView footerEndRefreshing];
        if (!error) {
            if ([dic[Code] intValue] == 200) {
                if (first == 1) {
                    _shuJuArr = [[NSMutableArray alloc]init];
                }
                NSArray * arr = dic[Response];
                [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    XXSC_FirstModel * firstModel = [XXSC_FirstModel objectFromDictionary:obj];
                    [_shuJuArr addObject:firstModel];
                }];
                if (_shuJuArr.count==0) {
                    [self noShop];
                }else{
                    [_view removeFromSuperview];
                }
                [_myTbaleView reloadData];
            }
        }
    }];
    
}


#pragma mark 没有商品
-(void)noShop{
     [_view removeFromSuperview];
    _view = [[[NSBundle mainBundle]loadNibNamed:@"XXSC_noShop" owner:self options:nil] lastObject];
    _view.frame = CGRectMake(0, 100, KScreenWidth, 127);
    [_myTbaleView addSubview:_view];
}
-(void)drawheaderOtherView{
    UIButton *searchBtn = [[UIButton alloc]initWithFrame:CGRectMake(50 , 27, KScreenWidth - 120  , 30)];
    searchBtn.backgroundColor = LIGHTGRAYBACKCOLOR;
    searchBtn.layer.cornerRadius = 4;
    [self.view addSubview:searchBtn];
    
    UIButton *fenLeiBtn = [[UIButton alloc]initWithFrame:CGRectMake(searchBtn.right + 15, searchBtn.top + 2.5, 25, 25)];
    [fenLeiBtn setImage:[UIImage imageNamed:@"下拉"] forState:UIControlStateNormal];
    [fenLeiBtn addTarget:self action:@selector(fenLeiClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:fenLeiBtn];
    
}

-(void)drawView{
    _myTbaleView = [[RWScrollView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight - 64)];
    _myTbaleView.delegate = self;
    _myTbaleView.dataSource = self;
    _myTbaleView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_myTbaleView];
    [_myTbaleView addHeaderWithTarget:self action:@selector(headRefresh)];
    [_myTbaleView addFooterWithTarget:self action:@selector(footerRefresh)];
    _fenLeiView = [[XXSPFenLeiTableView alloc]initWithFrame:CGRectMake(KScreenWidth, 64, KScreenWidth, KScreenHeight - 64)];
    _fenLeiView.delegate = self;
    [self.view addSubview:_fenLeiView];
}

-(void)fenLeiClicked:(UIButton *)sender{
    if (_fenLeiView.xv == KScreenWidth) {
        [UIView animateWithDuration:0.5 animations:^{
            _fenLeiView.xv = 0;
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _fenLeiView.xv = KScreenWidth;
        }];
    }
}

-(void)fenLeiTapClicked:(NSDictionary *)arr{
    [UIView animateWithDuration:0.5 animations:^{
        _fenLeiView.xv = KScreenWidth;
    }];
    _dic = arr;
    [_myTbaleView headerBeginRefreshing];
}

#pragma mark tableViewDataScorce
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _shuJuArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XXSC_FirstModel * firstModel1 = _shuJuArr[indexPath.row];
    XXSPMainTableCell * cell = [tableView dequeueReusableCellWithIdentifier:@"XXSPMainTableCell"];
    if (cell == nil) {
        cell = [[XXSPMainTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"XXSPMainTableCell"];
    }
    cell.nameLabel.text = firstModel1.shopName;
    cell.jianJieLabel.text = firstModel1.xiangxiDescribe;
    cell.renJunJiaLabel.text= [NSString stringWithFormat:@"人均价:￥%@",firstModel1.averageConsumption];
    cell.maiDanBtn.tag = indexPath.row;
    [cell.showImageView sd_setImageWithURL:[NSURL URLWithString:firstModel1.minImgURL]];
    [cell.maiDanBtn addTarget:self action:@selector(maiDanBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100 * _kscH;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView cellForRowAtIndexPath:indexPath].selected = NO;
    XXSPInfoViewController * chanPinInfo = [[XXSPInfoViewController alloc]init];
     XXSC_FirstModel * firstModel1 = _shuJuArr[indexPath.row];
    chanPinInfo.firstModel = firstModel1;
    [self.navigationController pushViewController:chanPinInfo animated:YES];
}

-(void)maiDanBtn:(UIButton *)sender{
     XXSC_FirstModel * firstModel1 = _shuJuArr[sender.tag];
    XXSPshopViewController * shop = [[XXSPshopViewController alloc]init];
    shop.firstModel = firstModel1;
    [self.navigationController pushViewController:shop animated:YES];
  }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
